package com.movie.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.movie.model.auto.FindPd;
import com.movie.model.auto.Role;
import com.movie.model.auto.User;
import com.movie.service.IUserService;
import com.movie.untils.SMS.SMSUtil;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author 达瓦里氏
 * @since 2021-12-16
 */
@RestController
@RequestMapping("/user")
@CrossOrigin
public class UserController {

    @Autowired
    private IUserService iUserService;

    /*查询用户*/
    @GetMapping("/getUser")
    public List<User> getUser(){
        return iUserService.list();
    }

    /*通过id查询用户*/
    @GetMapping("/getUserById/{id}")
    public User getUserById(@PathVariable("id") int id){
        return iUserService.getById(id);
    }

    /*修改用户*/
    @PostMapping("/updateUser")
    public void updateUser(@RequestBody User user){
        iUserService.updateById(user);
    }

    /*添加用户*/
    @PostMapping("/setUser")
    public void setUser(@RequestBody User user){
        iUserService.save(user);
    }

    /*删除用户*/
    @PostMapping("/deletedById/{id}")
    public boolean deleted(@PathVariable("id") int id){
        return iUserService.removeById(id);
    }

    //点击发送验证码
    @PostMapping("/sendMs/{phone}")
    public String sendMs (HttpServletRequest request, @PathVariable("phone") String phoneNumber){
        if(phoneNumber!=null&&!phoneNumber.equals("")){
            String s = SMSUtil.sendSMS(request,phoneNumber);
            request.getSession().setAttribute("Code",s);
            System.out.println(s);
            return s;
        }else{
            return "error";
        }
    }

    //验证码验证
    @PostMapping("/register")
    public Object register(HttpServletRequest request, @RequestBody FindPd findPd) {
        System.out.println("进来了！"+findPd);
        JSONObject json = (JSONObject)request.getSession().getAttribute("MsCode");
        if(!json.getString("key").equals(findPd.getCode())){
            return "验证码错误！";
        }
        //我这里模拟了一分钟
        if((System.currentTimeMillis() - json.getLong("createTime")) > 1000 * 60 * 1){
            return "验证码过期！";
        }
        return "验证成功！";
    }

    /*更新密码*/
    @PostMapping("/updatePassword")
    public void updatePassword(@RequestBody User user){
        //把电话号码对应的用户密码更新，其他属性不变
        System.out.println(user);
        UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("phone",user.getPhone());
        User user1 = new User();
        user1.setPassword(user.getPassword());
        iUserService.update(user1, updateWrapper);
    }


    /*前台用户登录*/
    @PostMapping("/login")
    public int Login(@RequestBody User user) {
        System.out.println(user.getName());
        System.out.println(user.getPassword());
        User login = iUserService.login(user.getName(), user.getPassword());
        System.out.println(login.getId());
        if (login != null) {
            int id = login.getId();
            int a = Math.toIntExact(id);
            return a;
        } else {
            return -1;
        }
    }

    /*后台登录*/
    @PostMapping("/admin")
    public int selectAdmin(@RequestBody User user){
        System.out.println(user);
        int i = 0;
        String name = user.getName();
        System.out.println(name);
        try{
            i = iUserService.selectAdmin(name);
        }catch (Exception e){
            return -1;
        }
        if(i==1){
            User login = iUserService.login(user.getName(), user.getPassword());
            if (login != null) {
                int id = login.getId();
                int a = Math.toIntExact(id);
                return a;
            } else {
                return -1;
            }
        }else {
            return -1;
        }
    }


    /*模糊查询用户数据*/
    @PostMapping("/selectUser/{input}")
    public List<Map<String, Object>> selectUser(@PathVariable("input") String value){
        System.out.println(value);
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        /*核心模糊算法*/
        queryWrapper.like("name",value);
        List<Map<String, Object>> maps = iUserService.listMaps(queryWrapper);
        return maps;
    }

    /*批量删除用户*/
    @PostMapping("/deletedAllById")
    public boolean deletedAllById(@RequestBody int[] delarr){
        for (int i : delarr) {
            System.out.println(i);
            iUserService.removeById(i);
        }
        return true;
    }

}
